package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.domain.Destination;
import cn.wolfcode.luowowo.domain.Region;
import cn.wolfcode.luowowo.query.RegionQuery;
import cn.wolfcode.luowowo.service.IDestinationService;
import cn.wolfcode.luowowo.service.IRegionService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

//区域控制器
@Controller
@RequestMapping("region")
public class RegionController {
    //注入区域业务
    @Autowired
    private IRegionService regionService;
    //注入目的地业务
    @Autowired
    private IDestinationService destinationService;

    //区域分页查询
    @RequestMapping("/list")
    public String list(Model model , @ModelAttribute("qo") RegionQuery qo){
        //Page是spring-data-mongodb 里封装分页参数的对象.类似pegeinfo
        Page<Region> page = regionService.list(qo);
        model.addAttribute("page" , page);
        return "region/list";
    }



    //新增/编辑
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Region region){
        if (StringUtils.hasLength(region.getId())){
            //带id则是编辑
            regionService.update(region);
        }else {
            //没带则是新增
            regionService.save(region);
        }
        //返回结果状态
        return JsonResult.success();
    }


    /**
     *
     *搜索下级目的地的所有选项
     * @param deep
     * @return
     */
    @RequestMapping("/getDestByDeep")
    @ResponseBody
    public Object getDestByDeep(int deep){
        List<Destination> list = destinationService.getDestByDeep(deep);
        //返回结果集
        return list;
    }

    //关联目的地的查询
    @RequestMapping("/getDestByRegionId")
    @ResponseBody
    public Object getDestByRegionId(String rid){
        //区域业务和目的地业务都能查实现,返回值是目的地推荐在目的地实现
        List<Destination> list = destinationService.getDestByRegionId(rid);
        //返回结果集
        return list;
    }


    //删除
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(String id){
        regionService.delete(id);
        //返回结果状态
        return JsonResult.success();
    }


    //设置热门与否    hot:hot, id:id
    @RequestMapping("/changeHotValue")
    @ResponseBody
    public Object changeHotValue(int hot , String id){
        regionService.changeHotValue(hot,id);
        //返回结果状态
        return JsonResult.success();
    }


}
